﻿using CatUtils.Utils.Currency;

using CatUtilsDll.Utils.Currency.WebUtils;
using Newtonsoft.Json;

using System;
using System.Collections.Generic;

namespace RabbitDownload.WebSite.Pixiv.PixivRelevant
{
    /// <summary>
    /// 调用P站相关推荐API，生成推荐
    /// </summary>
    internal class PixivReleVantHelper
    {
        /// <summary>
        /// 调用P站相关推荐API，生成推荐
        /// </summary>
        /// <param name="PixivID"></param>
        /// <param name="cWebclient"></param>
        /// <param name="DownloadMode">选择相关推荐的模式</param>
        public void StartScanner(string PixivID, CWebclient cWebclient, string DownloadMode)
        {
            string API = "https://www.pixiv.net/ajax/illust/{0}/recommend/init?limit=180";
            List<string> ImgList = new List<string>();
            int SkipCount = GetSkipCount(DownloadMode);
            try
            {
                string Shtml = cWebclient.GetHtml(string.Format(API, PixivID));
                PixivRelevantJson.Root root = JsonConvert.DeserializeObject<PixivRelevantJson.Root>(Shtml);
                foreach (var item in root.body.illusts)
                {
                    //https://i.pximg.net/img-original/img/2018/07/28/00/01/37/69888657_p0.png
                    string ImgUrl = item.imageUrl.Replace("c/360x360_70/img-master/", "img-original/").Replace("_square1200.jpg", "");
                    Console.WriteLine(ImgUrl);
                    ImgUrl = PixivHelper.GetImg(ImgUrl);
                    Console.WriteLine(ImgUrl);
                    if (ImgUrl != "")
                    {
                        ImgList.Add(ImgUrl);
                        Console.WriteLine("已扫描{0}张图。", ImgList.Count);
                    }
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            ImgList = AutoSplit(ImgList, DownloadMode, SkipCount);
            WebCurrency.SaveHelper.AutoSave(ImgList, "P站相关推荐ID", PixivID);
        }

        /// <summary>
        /// 根据选择的模式，切割图片列表。
        /// </summary>
        /// <param name="ImgList"></param>
        /// <param name="DownloadMode"></param>
        /// <param name="SkipCount"></param>
        /// <returns></returns>
        private List<string> AutoSplit(List<string> ImgList, string DownloadMode, int SkipCount)
        {
            if (SkipCount > 0 && ImgList.Count > SkipCount)
            {
                List<List<string>> DataList = ListHelper.ListSkip(ImgList, SkipCount);
                if (DataList.Count > 0)
                    return DataList[0];
            }
            return ImgList;
        }

        /// <summary>
        /// 获取多少次跳出
        /// </summary>
        /// <param name="DownloadMode"></param>
        /// <returns></returns>
        private static int GetSkipCount(string DownloadMode)
        {
            int SkipCount = 0;
            switch (DownloadMode)
            {
                case "相关性最强":
                    SkipCount = 60;
                    break;

                case "相关性中等":
                    SkipCount = 120;
                    break;

                case "相关性普通":
                    SkipCount = 180;
                    break;
            }

            return SkipCount;
        }
    }
}